<template>
  <div class="login">
    <div class="left">
      <div class="title-box">
        <img src="@/assets/img/login_logo.png" alt="" />
        <div class="title" style="color: #409eff">{{ $t('login.title') }}</div>
        <div class="line" />
        <div class="sub-title">{{ $t('login.userlogin') }}</div>
        <Language class="sonCom" />
      </div>
      <el-form ref="form" :model="form" :rules="rules" class="login-form">
        <el-form-item prop="phone">
          <el-input
            v-model="form.phone"
            prefix-icon="el-icon-user"
            :placeholder="$t('login.p1')"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-lock"
            :placeholder="$t('login.p2')"
            show-password
          />
        </el-form-item>
        <el-row :gutter="10">
          <el-col :span="16">
            <el-form-item prop="code">
              <el-input
                v-model="form.code"
                prefix-icon="el-icon-key"
                :placeholder="$t('login.p3')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <img
              v-if="showCode"
              style="width: 100%; height: 40px; cursor: pointer"
              :src="codeUrl"
              alt=""
              @click="changeCode"
            />
          </el-col>
        </el-row>
        <el-form-item prop="checked">
          <el-checkbox v-model="form.checked">{{
            $t('login.agree')
          }}</el-checkbox>
          <el-link
            type="primary"
            href="https://www.bilibili.com/"
            target="blank"
            >{{ $t('login.user') }}</el-link
          >
          {{ $t('login.and') }}
          <el-link
            type="primary"
            href="https://gitee.com/MarvinSuper/heimamm-xcc/"
            target="blank"
            >{{ $t('login.myself') }}</el-link
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="onSubmit">{{
            $t('login.login')
          }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="registerBtn">{{
            $t('login.register')
          }}</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="right">
      <img src="@/assets/img/login_banner_ele.png" alt="" />
    </div>
    <RegisterCom ref="RegisterCom" />
  </div>
</template>

<script>
import RegisterCom from './components/RegisterCom.vue'
export default {
  components: {
    RegisterCom
  },
  data () {
    return {
      codeUrl: process.env.VUE_APP_URL + '/captcha?type=login',
      showCode: true,
      form: {
        phone: '18511111111',
        password: '12345678',
        code: '',
        checked: false // 是否勾选
      },
      rules: {
        phone: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error(this.$t('login.tip1')))
              }
              const reg = /^1[3456789][0-9]{9}$/
              if (!reg.test(value)) {
                return callback(new Error(this.$t('login.checkTip1')))
              }
              callback()
            },
            trigger: 'change'
          }
        ],
        password: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error(this.$t('login.tip2')))
              }
              callback()
            },
            trigger: 'blur'
          },
          {
            min: 6,
            max: 16,
            message: this.$t('login.checkTip2'),
            trigger: 'change'
          }
        ],
        code: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error(this.$t('login.tip3')))
              }
              callback()
            },
            trigger: 'blur'
          },
          {
            min: 4,
            max: 4,
            message: this.$t('login.checkTip3'),
            trigger: 'change'
          }
        ],
        checked: [
          {
            required: true,
            validator: (rule, value, callback) => {
              value
                ? callback()
                : callback(new Error(this.$t('login.checkTip4')))
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    // 刷新验证码
    changeCode () {
      this.showCode = false
      this.$nextTick(() => {
        this.showCode = true
      })
    },
    onSubmit () {
      this.$refs.form.validate(async result => {
        if (result) {
          const bol = await this.$store.dispatch('login/login', this.form)
          if (bol) {
            this.$message.success('登录成功')
            this.$router.push('/')
          } else {
            this.changeCode()
          }
        }
      })
    },
    // 点击注册打开弹框
    registerBtn () {
      this.$refs.RegisterCom.show = true
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: linear-gradient(225deg, #1493fa, #01c6fa);
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 48px;
    position: relative;
    .sonCom {
      position: absolute;
      top: 47px;
      right: 50px;
    }
    .title-box {
      display: flex;
      align-items: center;
      img {
        width: 22px;
        height: 17px;
        margin-right: 16px;
      }
      .title {
        font-size: 24px;
        margin-right: 14px;
      }
      .line {
        width: 1px;
        background-color: #c7c7c7;
        height: 27px;
        margin-right: 12px;
      }
      .sub-title {
        font-size: 22px;
      }
    }
    .login-form {
      margin-top: 29px;
    }
  }
}
</style>
